-- http://logging.admicro.vn/_cpm4k.js
local biz_name = "_cpm4k.js"

if test then
    ConfigRedisConnectors[biz_name]={
        host = test.host,
        port = test.port,
    }
else
    ConfigRedisConnectors[biz_name]={
        --host = "192.168.2.28",
        --port = 6379,
        host = "192.168.5.201",
        port = 6380,
    }
end

local function _cpm4k_js()
return coroutine.create(function ()
-- constants
local get_cmd_format = "get %s"
local http_server_name = config.http.servername

-- headers
local local_config = config
local SERVER_HEADER = local_config.SERVER_HEADER
local COOKIE_HEADER = local_config.COOKIE_HEADER
local PRAGMA_HEADER = local_config.PRAGMA_HEADER
local CACHE_CONTROL_HEADER = local_config.CACHE_CONTROL_HEADER
local EXPIRES_HEADER = local_config.EXPIRES_HEADER
local CONTENT_TYPE_HEADER = local_config.CONTENT_TYPE_HEADER
--cookies
local GUID_COOKIE = local_config.GUID_COOKIE
-- functions
local reply_pushHeader = reply.pushHeader
local reply_ok = reply.status_type.ok
local reply_setStatus = reply.setStatus
local reply_appendContent = reply.appendContent
local request_getHeadersKV = request.getHeadersKV
local os_time = os.time
local os_date = os.date
local luredis_reply_string = luredis.reply_type.string
local util_redis_cmd = util.redis_cmd
local util_parseKV = util.parseKV
local luredis_send_cmd = luredis.send_cmd
local coroutine_yield = coroutine.yield

while true do
    -- reply
    reply_pushHeader(CACHE_CONTROL_HEADER, "max-age=300, must-revalidate")
    reply_pushHeader(PRAGMA_HEADER, "no-cache")
    reply_pushHeader(EXPIRES_HEADER, os_date("%a, %d %b %Y %H:%M:%S",os_time() + 300))
    reply_pushHeader(CONTENT_TYPE_HEADER, "text/javascript")
    reply_pushHeader("P3P", "CP=\"IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA\"")
    reply_pushHeader(SERVER_HEADER, http_server_name)
    reply_setStatus(reply_ok)
    -- headers
    local headerTable = request_getHeadersKV();
    -- cookie
    local cookieTable = util_parseKV(headerTable[COOKIE_HEADER],";=");
    -- get guid
    local guid = cookieTable[GUID_COOKIE];
    reply_appendContent("var __arCPM4K = [")
    if guid then
        local result = util_redis_cmd(biz_name,false,
                                    luredis_send_cmd,get_cmd_format, guid);
        if type(result) == "string" then
            reply_appendContent(result)
        end
    end
    reply_appendContent("];")
    coroutine_yield()
end
end)
end

creators[biz_name] = function()
    f[biz_name] = _cpm4k_js()
end
